var module = angular.module('app.VipController', []);

module.controller('VipController',function ($scope, ConfigModule, $http, $location) {
    //$location.path('/order/index');
    $scope.$emit('hideNav',false);

});

module.controller('VipIndexController',function ($scope, API,$localStorage,$ionicPopup,$window,$base64) {
    //$location.path('/order/index');

    API.vipPrivilege(function(err,result){
        $scope.vipinfo = result.data;
    });
    $scope.userinfo = $localStorage.userinfo;
    $scope.$emit('hideNav',false);
    // $scope.$emit('hideNav',true);

    $scope.pay_type = {};
    $scope.pay_type.pay_type = 1;

    $scope.alert = function(msg){
        var confirm = $ionicPopup.confirm({
            title: '消息提示',
            template : msg,
            buttons: [
                {
                    text: '<b>确定</b>',
                    type: 'button-positive'
                }
            ]
        });
    };
    $scope.pay = function(vip_type){

        var confirm = $ionicPopup.confirm({
            title: '选择支付方式',
            templateUrl: 'tpl/public/check_pay2.html',
            okText : '确定',
            cancelText : '取消',
            scope : $scope
        });
        confirm.then(function(res) {
            if(res) {
                if($scope.pay_type.pay_type){
                    API.vipRecharge({
                        vip_type : vip_type,
                        pay_type : $scope.pay_type.pay_type
                    },function(err,result){
                        if(result.status == 200){
                            if($scope.pay_type.pay_type == 1){
                                var wepay = result.data;
                                wepay.vip = true;
                                var info = angular.toJson(wepay);
                                $window.location.href='http://www.hjfzqy.cn/#/pay?info='+$base64.encode(info);
                            }else{
                                $scope.alert('支付成功');
                                $state.reload();
                            }
                        }else{
                            $scope.alert(result.message);
                        }
                    });
                }
            }
        });
    };
});


module.controller('VipDetailController',function ($scope, API,$localStorage) {
    //$location.path('/order/index');

    API.getVipAccount({
        pagesize : 100
    },function(err,result){
        $scope.total_money = result.data.total_money;
        $scope.VipAccount = result.data.VipAccount;
    });
    $scope.$emit('hideNav',false);

});

module.controller('VipPayController',function ($scope, API,$ionicPopup,$window,$base64) {
    //$location.path('/order/index');
    $scope.$emit('hideNav',true);

    $scope.pay_type = {};
    $scope.pay_type.pay_type = 1;

    $scope.alert = function(msg){
        var confirm = $ionicPopup.confirm({
            title: '消息提示',
            template : msg,
            buttons: [
                {
                    text: '<b>确定</b>',
                    type: 'button-positive'
                }
            ]
        });
    };
    $scope.pay = function(vip_type){

        var confirm = $ionicPopup.confirm({
            title: '选择支付方式',
            templateUrl: 'tpl/public/check_pay2.html',
            okText : '确定',
            cancelText : '取消',
            scope : $scope
        });
        confirm.then(function(res) {
            if(res) {
                if($scope.pay_type.pay_type){
                    API.vipRecharge({
                        vip_type : vip_type,
                        pay_type : $scope.pay_type.pay_type
                    },function(err,result){
                        if(result.status == 200){
                            if($scope.pay_type.pay_type == 1){
                                var wepay = result.data;
                                wepay.vip = true;
                                var info = angular.toJson(wepay);
                                $window.location.href='http://www.hjfzqy.cn/#/pay?info='+$base64.encode(info);
                            }else{
                                $scope.alert('支付成功');
                                $state.reload();
                            }
                        }else{
                            $scope.alert(result.message);
                        }
                    });
                }
            }
        });
    };
});
//
// module.controller('VipCashPayController',function ($scope,API,$ionicModal,$ionicPopup,$state,$window,$base64) {
//     //$location.path('/order/index');
//     $scope.$emit('hideNav',true);
//     API.getStore({id : '57d1177feaff7c6e18c03a0d'},function(err,result){
//         // if(result.data && result.data[0]){
//         //     result.data[0].checked = true;
//         //     $scope.checked = result.data[0];
//         // }
//         $scope.stores = result.data;
//         $scope.checked = {};
//         $scope.checked.store_address = '请选择支付门店';
//
//     });
//     $scope.select_pay = function(action){
//         if(action == 'wepay'){
//             $scope.pay_type.wepay = 'active';
//             $scope.pay_type.yue = '';
//             $scope.pay_type.vip = '';
//             $scope.pay_type.pay_type = '1';
//             $scope.count_cash();
//         }else if(action == 'yue'){
//             $scope.pay_type.wepay = '';
//             $scope.pay_type.yue = 'active';
//             $scope.pay_type.vip = '';
//             $scope.pay_type.pay_type = '2';
//             $scope.count_cash();
//         }else{
//             $scope.pay_type.wepay = '';
//             $scope.pay_type.yue = '';
//             $scope.pay_type.vip = 'active';
//             $scope.pay_type.pay_type = '3';
//             $scope.count_cash();
//         }
//     };
//     API.vipPrivilege(function(err,result){
//         var vipinfo = result.data;
//         vipinfo.vip_discount = (parseFloat(vipinfo.vip_discount)*10).toFixed(1);
//         $scope.vipinfo = vipinfo;
//     });
//     API.getPostal(function(err,result){
//         $scope.postal = result;
//     });
//     $ionicModal.fromTemplateUrl('tpl/vip/select_address.html', {
//         scope: $scope,
//         animation: 'slide-in-left'
//     }).then(function(modal) {
//         $scope.address_list = modal;
//     });
//     $scope.pay_price = 0;
//     $scope.pay_price = $scope.pay_price.toFixed(2);
//     $scope.open_address = function($event){
//         $scope.address_list.show($event);
//     };
//     $scope.cash_num = {};
//     $scope.count_cash = function(){
//         if($scope.pay_type.pay_type == 1){
//             if(parseFloat($scope.cash_num.price) >= 0){
//                 $scope.pay_price = parseFloat($scope.cash_num.price).toFixed(2);
//             }
//         }else if($scope.pay_type.pay_type == 2){
//             if(parseFloat($scope.cash_num.price) >= 0){
//                 $scope.pay_price = parseFloat($scope.cash_num.price).toFixed(2);
//             }
//         }else{
//             if(parseFloat($scope.cash_num.price) >= 0){
//                 $scope.pay_price = parseFloat($scope.cash_num.price)*parseFloat($scope.vipinfo.vip_discount/10);
//                 $scope.pay_price = $scope.pay_price.toFixed(2);
//             }
//         }
//     };
//     $scope.check_address_close = function($event){
//         $scope.address_list.hide($event);
//     };
//
//     $scope.alert = function(msg){
//         var confirm = $ionicPopup.confirm({
//             title: '消息提示',
//             template : msg,
//             buttons: [
//                 {
//                     text: '<b>确定</b>',
//                     type: 'button-positive'
//                 }
//             ]
//         });
//     };
//     $scope.pay_type = {};
//     $scope.pay_type.pay_type = 1;
//     $scope.pay_type.wepay = 'active';
//     $scope.pay_type.yue = '';
//     $scope.pay_type.vip = '';
//     $scope.pay = function(){
//         if(!$scope.checked._id){
//             return $scope.alert('请选择支付门店');
//         }
//         if(parseFloat($scope.pay_price) <= 0){
//             return $scope.alert('请输入正确的数字');
//         }
//         if($scope.pay_type.pay_type == 3){
//             if(parseFloat($scope.pay_price) >= parseFloat($scope.vipinfo.vip_postal)){
//                 return $scope.alert('VIP余额不足');
//             }
//             pay_money = $scope.pay_price;
//         }
//         if($scope.pay_type.pay_type == 2){
//             if(parseFloat($scope.pay_price) >= parseFloat($scope.postal.money)){
//                 return $scope.alert('余额不足');
//             }
//             pay_money = $scope.cash_num.price;
//         }
//         if($scope.pay_type.pay_type == 1 ){
//             pay_money = $scope.cash_num.price;
//
//         }
//         API.storePay({
//             pay_money : pay_money,
//             pay_type : $scope.pay_type.pay_type,
//             store_id : $scope.checked._id
//         },function(err,result){
//
//             if(result.status == 200){
//                 if($scope.pay_type.pay_type == 1){
//                     var wepay = result.data.wepay;
//                     wepay.vip_cach = true;
//                     var info = angular.toJson(wepay);
//                     $window.location.href='http://www.hjfzqy.cn/#/pay?info='+$base64.encode(info);
//                 }else{
//                     $scope.alert('支付成功');
//                     $state.reload();
//                 }
//             }else{
//                 $scope.alert('支付失败，余额不足');
//             }
//         });
//         // var confirm = $ionicPopup.confirm({
//         //     title: '选择支付方式',
//         //     templateUrl: 'tpl/public/check_pay.html',
//         //     okText : '确定',
//         //     cancelText : '取消',
//         //     scope : $scope
//         // });
//         // confirm.then(function(){
//         //     var pay_money = 0;
//         //
//         // });
//     };
//     $scope.check_address = function(info){
//         $scope.checked = info;
//         for(i in $scope.stores){
//             if(info._id == $scope.stores[i]._id){
//                 $scope.stores[i].checked = true;
//             }else{
//                 $scope.stores[i].checked = false;
//             }
//         }
//
//         $scope.address_list.hide();
//     };
// });






